#include "salarydetailview.h"
#include "employeemanager.h"
#include <QHeaderView>
#include <QMessageBox>
#include <QDebug>

SalaryDetailView::SalaryDetailView(EmployeeManager *manager, const QString &empId, QWidget *parent) : QWidget(parent),
    m_manager(manager),
    m_empId(empId),
    m_salaryTable(nullptr),
    m_statusLabel(nullptr),
    m_startDateEdit(nullptr),
    m_endDateEdit(nullptr),
    m_searchButton(nullptr),
    m_periodComboBox(nullptr)
{
    initUI();
    if (!m_empId.isEmpty()) {
        loadSalaryDetails();
    }
}

SalaryDetailView::~SalaryDetailView()
{
    // 析构函数，释放动态分配的内存
    delete m_salaryTable;
    delete m_statusLabel;
    delete m_startDateEdit;
    delete m_endDateEdit;
    delete m_searchButton;
    delete m_periodComboBox;
}

void SalaryDetailView::setEmployeeId(const QString &empId)
{
    m_empId = empId;
    loadSalaryDetails();
}

void SalaryDetailView::initUI()
{
    // 创建主布局
    QVBoxLayout *mainLayout = new QVBoxLayout(this);

    // 创建搜索区域
    QWidget *searchWidget = new QWidget(this);
    QHBoxLayout *searchLayout = new QHBoxLayout(searchWidget);

    // 期间选择下拉框
    m_periodComboBox = new QComboBox(this);
    m_periodComboBox->addItem("全部");
    m_periodComboBox->addItem("近一个月");
    m_periodComboBox->addItem("近三个月");
    m_periodComboBox->addItem("近半年");
    m_periodComboBox->addItem("近一年");

    // 搜索按钮
    m_searchButton = new QPushButton("搜索", this);
    connect(m_searchButton, &QPushButton::clicked, this, &SalaryDetailView::onSearchSalary);

    // 添加到搜索布局
    searchLayout->addWidget(new QLabel("期间: ", this));
    searchLayout->addWidget(m_periodComboBox);
    searchLayout->addWidget(m_searchButton);
    searchLayout->addStretch();

    // 创建工资表格
    m_salaryTable = new QTableWidget(this);
    m_salaryTable->setColumnCount(7);
    m_salaryTable->setHorizontalHeaderLabels({"工资ID", "员工ID", "基本工资", "扣款", "实发工资", "发放日期", "发放期间"});
    m_salaryTable->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch);
    m_salaryTable->setEditTriggers(QTableWidget::NoEditTriggers);

    // 状态标签
    m_statusLabel = new QLabel("请选择查询条件", this);

    // 添加到主布局
    mainLayout->addWidget(searchWidget);
    mainLayout->addWidget(m_salaryTable);
    mainLayout->addWidget(m_statusLabel);

    setLayout(mainLayout);
    setWindowTitle("工资详情");
    resize(800, 600);
}


void SalaryDetailView::loadSalaryDetails()
{
    // 默认加载全部工资数据
    m_periodComboBox->setCurrentIndex(0); // 选择'全部'
    onSearchSalary();
}

void SalaryDetailView::onSearchSalary()
{
    if (m_manager == nullptr || m_empId.isEmpty()) {
        m_statusLabel->setText("未设置员工信息");
        return;
    }

    // 获取选择的期间
    int periodIndex = m_periodComboBox->currentIndex();
    QString startDate, endDate;
    QVariantList salaryDetails;

    // 根据选择的期间设置日期范围
    switch (periodIndex) {
    case 0: // 全部
        salaryDetails = m_manager->getSalaryDetails(m_empId);
        break;
    case 1: // 近一个月
        startDate = QDate::currentDate().addMonths(-1).toString("yyyy-MM-dd");
        endDate = QDate::currentDate().toString("yyyy-MM-dd");
        salaryDetails = m_manager->getSalaryDetailsByDateRange(m_empId, startDate, endDate);
        break;
    case 2: // 近三个月
        startDate = QDate::currentDate().addMonths(-3).toString("yyyy-MM-dd");
        endDate = QDate::currentDate().toString("yyyy-MM-dd");
        salaryDetails = m_manager->getSalaryDetailsByDateRange(m_empId, startDate, endDate);
        break;
    case 3: // 近半年
        startDate = QDate::currentDate().addMonths(-6).toString("yyyy-MM-dd");
        endDate = QDate::currentDate().toString("yyyy-MM-dd");
        salaryDetails = m_manager->getSalaryDetailsByDateRange(m_empId, startDate, endDate);
        break;
    case 4: // 近一年
        startDate = QDate::currentDate().addYears(-1).toString("yyyy-MM-dd");
        endDate = QDate::currentDate().toString("yyyy-MM-dd");
        salaryDetails = m_manager->getSalaryDetailsByDateRange(m_empId, startDate, endDate);
        break;
    default:
        salaryDetails = m_manager->getSalaryDetails(m_empId);
        break;
    }

    // 清空表格
    m_salaryTable->clearContents();
    m_salaryTable->setRowCount(0);

    // 填充表格
    if (salaryDetails.isEmpty()) {
        m_statusLabel->setText("没有找到指定日期范围内的工资记录");
        return;
    }

    m_salaryTable->setRowCount(salaryDetails.size());

    for (int i = 0; i < salaryDetails.size(); ++i) {
        QVariantHash salaryInfo = salaryDetails[i].toHash();

        // 填充表格行
        m_salaryTable->setItem(i, 0, new QTableWidgetItem(salaryInfo["salary_id"].toString()));
        m_salaryTable->setItem(i, 1, new QTableWidgetItem(salaryInfo["emp_id"].toString()));
        m_salaryTable->setItem(i, 2, new QTableWidgetItem(salaryInfo["basic_salary"].toString()));
        m_salaryTable->setItem(i, 3, new QTableWidgetItem(salaryInfo["deduction"].toString()));
        m_salaryTable->setItem(i, 4, new QTableWidgetItem(salaryInfo["actual_salary"].toString()));
        m_salaryTable->setItem(i, 5, new QTableWidgetItem(salaryInfo["payment_date"].toString()));
        m_salaryTable->setItem(i, 6, new QTableWidgetItem(salaryInfo["payment_period"].toString()));
    }

    m_statusLabel->setText(QString("共找到 %1 条工资记录").arg(salaryDetails.size()));
}
